﻿using System.Collections.Generic;
using System.ComponentModel;

namespace Phylogeny.Dendogram
{
    public class DendoNode<T> : DendoNodeBase, INotifyPropertyChanged
    {

        private DendoNodes<T> _subNodes = new DendoNodes<T>();
        public DendoNodes<T> SubNodes
        {
            get
            {
                return _subNodes;
            }
        }

        private T _item;
        public T Item
        {
            get
            {
                return _item;
            }
            set
            {

                _item = value;
                OnPropertyChanged("Item");
                OnPropertyChanged("IsEmpty");
            }
        }

        public bool IsEmpty
        {
            get
            {
                return _item == null;
            }
        }


        DendoNodes<T> _collection; //the owning collection of this node
        public DendoNode(DendoNodes<T> owner, TreeConfig conf)
            : base(conf)
        {
            _collection = owner;

            if (_collection != null)
            {
                _collection.Add(this);

            }
            _subNodes.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_subNodes_CollectionChanged);
        }

        public DendoNode(DendoNodes<T> owner, TreeConfig conf, bool isRoot)
            : this(owner, conf)
        {
            IsRoot = isRoot;
        }

        void _subNodes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged("IsLeaf");
            OnPropertyChanged("IsRightEdge");
            OnPropertyChanged("IsLeftEdge");
        }


        public override int SubNodeCount()
        {
            return _subNodes.Count;
        }

        public override int OwnerNodeCount()
        {
            if (_collection == null)
                return 0;
            else
                return _collection.Count;
        }

        public override DendoNodeBase FirstNode()
        {
            if (_collection == null && _collection.Count > 0)
                return null;
            else
                return _collection[0];
        }

        public override DendoNodeBase LastNode()
        {
            if (_collection == null && _collection.Count > 0)
                return null;
            else
                return _collection[_collection.Count - 1];
        }

        public override IEnumerable<DendoNodeBase> GetSubNodes()
        {
            List<DendoNodeBase> ret = new List<DendoNodeBase>();

            foreach (var item in _subNodes)
            {
                ret.Add(item);
            }

            return ret;
        }

        public override DendoNodeBase FirstChildNode()
        {
            if (_subNodes.Count > 0)
                return _subNodes[0];
            else
                return null;
        }


    }


}
